---------------------------------------
--            AmBFrame               --
---------------------------------------
-- Cool-ass frame thing. Intended to not be
-- parented to any other controls
local Frame = {
   Init = function(self)
      self.FrameColor = ThemeVar("default_frame_color") or Color(0, 0, 0, 255) --Color of the frame
      self.OutlineColor = ThemeVar("default_frame_outline_color") or Color(0, 0, 0, 255) --Color of the frames outline
      self.OutlineBlendColor = ThemeVar("default_frame_outline_blend_color") or Color(255, 0, 0, 0) --Color to blend the frame
      self.InteriorColor = ThemeVar("default_frame_interior_color") or Color(0, 0, 0, 192)
      self._OutlineThickness = ThemeVar("default_frame_outline_thickness") or 5
      self._MarginTop = ThemeVar("default_frame_margin_top") or 1 --Margin at the top of the frame
      self._Margin = ThemeVar("default_frame_margin") or 1 --Margin on all other sides

      --Content zones
      self._InteriorPanel = vgui.Create("AmBContentPanel", self)
      self._TitlePanel = vgui.Create("AmBContentPanel", self)
      self:_RecalculateZones()
   end,

   -- Sets the margins of the frame. first argument
   -- is the general margin size while the second is th
   -- top.
   SetMargin = function(self, Around, Top)
      self._Margin = Around
      self._MarginTop = Top
   end,

   Paint = function(self)
      --Draw outside frame
      SetSurfaceToColor(Freeze(self.FrameColor))
      local width = self:GetWide()
      local height = self:GetTall()
      local thick = Freeze(self._OutlineThickness)
      local margin = Freeze(self._Margin)
      local margintop = Freeze(self._MarginTop)
		thick = math.min(math.min(margin, margintop), thick)
      surface.DrawRect(margin, thick, width - (margin * 2), margintop - thick)
      surface.DrawRect(thick, thick, margin - thick, height - (thick * 2))
      surface.DrawRect(width - margin, thick, margin - thick, height - (thick * 2))
      surface.DrawRect(margin, height - margin, width - (margin * 2), margin - thick)

      --Draw interior
      SetSurfaceToColor(Freeze(self.InteriorColor))
      surface.DrawRect(margin, margintop, width - (margin * 2), height - (margintop + margin))

      --Draw borders for outside frame
		local obc = Freeze(self.OutlineBlendColor)
		local oc = Freeze(self.OutlineColor)
      if obc != nil then
         for x = 0, (thick - 1) do
            SetSurfaceToBlend(obc, oc, x / thick)
            surface.DrawOutlinedRect(x, x, width - (x * 2), height - (x * 2))
         end
      else
         SetSurfaceToColor(oc)
         for x = 0, (thick - 1) do
            surface.DrawOutlinedRect(x, x, width - (x * 2), height - (x * 2))
         end
      end

      return false
   end,

   PanelThink = function(self) 
      self:_RecalculateZones()
   end,

   GetInterior = function(self)
      return self._InteriorPanel
   end,

   GetTitleArea = function(self)
      return self._TitlePanel
   end,

   --Recalculates the content zones of the frame
   _RecalculateZones = function(self)
      local width = self:GetWide()
      local height = self:GetTall()
      local thick = Freeze(self._OutlineThickness)
      local margin = Freeze(self._Margin)
      local margintop = Freeze(self._MarginTop)

      self._InteriorPanel:SetPos(margin, margintop)
      self._InteriorPanel:SetSize(width - (margin * 2), height - (margin + margintop))
      
      self._TitlePanel:SetPos(margin, thick)
      self._TitlePanel:SetSize(width - (margin * 2), margintop - thick)
   end
}
AmB.UI.Frame = Frame
vgui.Register("AmBFrame", Frame, "AmBContentPanel")